'''
Created on Apr 28, 2010

@author: Juan
'''

import ConfigParser
import sys

class ConfigLoader(object):
    '''
    ConfigLoader Class
    ''' 
    
    def load(self, arg):
        '''
        Loads the configuration file into memory.
        '''
        config_data = {}
        config = ConfigParser.RawConfigParser()
        # Using readfp allows us to catch an IOError exception in case the file does not
        # exist. An exception handler should be added to make the error a bit friendlier
        # and to exit the agent
        try:
            config.readfp(open(arg))
        except IOError: # If file doesn't exist, print an error and exits.
            print 'Cannot open "', arg, '"'
            sys.exit()
    
        sections = config.sections()
        
        for s in sections:
            sopts = config.options(s)
            sdata = dict((sopt, config.get(s,sopt)) for sopt in sopts )
            config_data[s] = sdata
            
        return config_data